package com.bamanker.cloudapi.model.response;


import lombok.Data;

/**
 * @author bamanker
 * <p>
 * 统一响应结果处理  使用链式编程 返回类本身
 */
@Data
public class Result<T> {

    private Boolean success;

    private String code;

    private String message;

    private String request;
    /**
     * 接口请求时间戳
     */
    private Long timestamp;

    private T data;

    public Result(Boolean success, String code, String msg, T data) {
        this.success = success;
        this.code = code;
        this.message = msg;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    public Result(Boolean success, String code, String msg, String request, T data) {
        this.success = success;
        this.code = code;
        this.message = msg;
        this.request = request;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }


    private Result setSuccess(Boolean success) {
        this.success = success;
        return this;
    }


    private Result setMessage(String message) {
        this.message = message;
        return this;
    }

    private Result setData(T data) {
        this.data = data;
        return this;
    }

    private Result setCode(String code) {
        this.code = code;
        return this;
    }

    private Result setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
        return this;
    }

    public static <T> Result<T> build(Boolean success, ResultCode resultCode, T result) {
        return new Result(success, resultCode.getCode(), resultCode.getMessage(), result);
    }

    public static <T> Result<T> build(Boolean success, ResultCode resultCode, String request, T result) {
        return new Result(success, resultCode.getCode(), resultCode.getMessage(), request, result);
    }

    public static <T> Result<T> build(Boolean success, String code, String msg, T result) {
        return new Result(success, code, msg, result);
    }

    public static <T> Result<T> buildSuccess(String msg, T result) {
        return build(Boolean.TRUE, "OK", msg, result);
    }

    public static <T> Result<T> buildSuccess(T result) {
        return build(Boolean.TRUE, "OK", "操作成功", result);
    }

    public static <T> Result<T> buildSuccess() {
        return build(Boolean.TRUE, "OK", "操作成功", (T) null);
    }

    public static <T> Result<T> buildFailure(String code, String msg, T result) {
        return build(Boolean.FALSE, code, msg, result);
    }

    public static <T> Result<T> buildFailure(String code, String msg) {
        return build(Boolean.FALSE, code, msg, (T) null);
    }

    public static <T> Result<T> buildFailure(ResultCode resultCode, T result) {
        return build(Boolean.FALSE, resultCode, result);
    }

    public static <T> Result<T> buildFailure(ResultCode resultCode, String request) {
        return build(Boolean.FALSE, resultCode, request, (T) null);
    }

    public static <T> Result<T> buildFailure(ResultCode resultCode) {
        return build(Boolean.FALSE, resultCode, (T) null);
    }

//    /**
//     * 通用返回成功
//     *
//     * @return
//     */
//    public static <T> Result<T> success() {
//        return new Result(System.currentTimeMillis())
//                .setSuccess(Boolean.TRUE)
//                .setCode(HttpStatus.OK.value())
//                .setMessage("操作成功");
//    }
//
//    /**
//     * 通用返回失败
//     *
//     * @return
//     */
//    public static Result failure() {
//        return new Result(System.currentTimeMillis())
//                .setSuccess(ResultCodeEnum.FAIL.getSuccess())
//                .setCode(ResultCodeEnum.FAIL.getCode())
//                .setMessage(ResultCodeEnum.FAIL.getMessage());
//
//    }
//
//    /**
//     * 设置结果，形参为结果枚举
//     *
//     * @param result
//     * @return
//     */
//    public static Result setResult(CommonErrorCode result) {
//        return new Result(System.currentTimeMillis())
//                .setSuccess(result.getSuccess())
//                .setCode(result.getCode())
//                .setMessage(result.getMessage());
//
//    }
//
//
//    // 自定义返回数据
//    public Result data(Map<String, Object> map) {
//        return this.setData(map);
//
//    }
//
//    // 通用设置data
//    public Result data(String key, Object value) {
//        this.data.put(key, value);
//        return this;
//    }
//
//    // 自定义状态信息
//    public Result message(String message) {
//        return this.setMessage(message);
//
//    }
//
//    // 自定义状态码
//    public Result code(Integer code) {
//        return this.setCode(code);
//
//    }
//
//    // 自定义返回结果
//    public Result success(Boolean success) {
//        return this.setSuccess(success);
//
//    }

}